package com.qzsoft.zhjd.asphalt.modules.asphaltmatch.controller;

import java.util.List;
import java.util.Map;

import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.dto.AsphaltMatchDetsilsDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.dto.AsphaltMatchListDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.dto.AsphaltMatchSelectDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.qzsoft.jeemis.common.utils.Result;

import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;

/**
 * @ClassName AsphaltMatchController
 * @Description 沥青混合料掺生产配比控制器
 * @author linhuawei
 * @since 2021-09-03
 */
@RestController
@RequestMapping("/api/asphaltMatch")
public class AsphaltMatchController {

    @Autowired
    private AsphaltMatchService asphaltMatchService;

    @Autowired
    private SysStationService stationService;

    /**
     * @Title page
     * @Description 获取生产配比分页数据
     * @param params 查询条件
     * @return 生产配比分页数据
     */
    @RequiresPermissions({"api:asphaltMatch:list","api:test:configuration"})
    @GetMapping("/page")
    public Result<IPage<AsphaltMatchListDto>> page(@RequestParam Map<String, Object> params) {
        IPage<AsphaltMatchListDto> page = asphaltMatchService.pageDto(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title list
     * @Description 获取配方列表
     * @param params 查询条件
     * @return 配方列表
     */
    @GetMapping("/list")
    public Result<List<AsphaltMatchEntity>> list(@RequestParam Map<String, Object> params){
        List<AsphaltMatchEntity> list = asphaltMatchService.list(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title listSelect
     * @Description 获取配方列表
     * @param params 查询条件
     * @return 配方列表
     */
    @GetMapping("/listSelect")
    public Result<List<AsphaltMatchSelectDto>> listSelect(@RequestParam Map<String, Object> params){
        List<AsphaltMatchSelectDto> list = asphaltMatchService.listSelect(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title: find
     * @Description: 获取生产配比信息
     * @param id 生产配比主键
     * @return 生产配比信息
     */
    @RequiresPermissions("api:asphaltMatch:edit")
    @GetMapping("/find/{id}")
    public Result<AsphaltMatchEntity> find(@PathVariable("id") String id) {
        AsphaltMatchEntity entity = asphaltMatchService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title details
     * @Description 获取配比详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<AsphaltMatchDetsilsDto> details(@PathVariable("id") String id) {
        AsphaltMatchDetsilsDto dto = asphaltMatchService.getDetails(id);
        if (null == dto) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(dto);
    }

    /**
     * @Title: save
     * @Description: 添加生产配比信息
     * @param entity 生产配比信息
     * @return 添加结果
     */
    @RequiresPermissions("api:asphaltMatch:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody AsphaltMatchEntity entity) {
        ValidatorUtils.validateEntity(entity);
        SysStationEntity stationByCode = stationService.getStationByCode(entity.getStationCode());
        Assert.notNull(stationByCode,"拌合楼不存在！");
        entity.setBaseCode(stationByCode.getBaseCode());
        asphaltMatchService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: update
     * @Description: 修改生产配比信息
     * @param entity 生产配比信息
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody AsphaltMatchEntity entity) {
        ValidatorUtils.validateEntity(entity);
        asphaltMatchService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: delete
     * @Description: 删除生产配比信息
     * @param id 生产配比主键
     * @return 删除结果
     */
    @RequiresPermissions("api:asphaltMatch:remove")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        asphaltMatchService.removeById(id);
        return ResultUtils.ok();
    }

}
